﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;

namespace WebServiceVersion
{
    /// <summary>
    /// Summary description for ServiceChiTietMay
    /// </summary>
    [WebService(Namespace = "http://soiphubai.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ServiceChiTietMay : System.Web.Services.WebService
    {
        [WebMethod]
        public bool LogIn(string userName, string passWord)
        {
            var user = HeThong.GetNguoiSuDung(userName, passWord);
            return user == null ? false : true;
        }

        [WebMethod]
        public List<CongDoanSanXuatCustom> GetListCongDoan(string userName, string passWord)
        {
            var user = HeThong.GetNguoiSuDung(userName, passWord);
            if (user == null)
                return null;
            var manv = userName;
            bool allowAdmin = user.Role == (int)VAITRO.Admin;
            if (allowAdmin)
                manv = "";
            var lsCongDoan = BizCongDoanSanXuat.GetList();
            List<CongDoanSanXuatCustom> rs = new List<CongDoanSanXuatCustom>();
            foreach (var congDoan in lsCongDoan)
            {
                if (!allowAdmin)
                    if (!BizPhanCongBaoTri.CheckPermissionForCongDoan(manv, congDoan.MaCongDoanSanXuat))
                        continue;
                int trongHan = 0;
                int quaHan = 0;
                int truocHan = 0;
                BizCongDoanSanXuat.GetStatisCanhBao(congDoan.MaCongDoanSanXuat, out quaHan, out trongHan, out truocHan, manv);
                CongDoanSanXuatCustom cd = new CongDoanSanXuatCustom
                {
                    MaCongDoanSanXuat = congDoan.MaCongDoanSanXuat,
                    TenCongDoanSanXuat = congDoan.TenCongDoanSanXuat,
                    CanhBao = string.Format("{0}/{1}/{2}", quaHan, trongHan, truocHan)
                };
                rs.Add(cd);
            }
            return rs;
        }

        [WebMethod]
        public List<MaySanXuatCustom> GetListMaySanXuat(string userName, string passWord, string maCongDoan)
        {
            var user = HeThong.GetNguoiSuDung(userName, passWord);
            if (user == null)
                return null;
            var manv = userName;
            bool allowAdmin = user.Role == (int)VAITRO.Admin;
            if (allowAdmin)
                manv = "";
            List<MaySanXuatCustom> rs = new List<MaySanXuatCustom>();
            var congDoan = BizCongDoanSanXuat.GetByID(maCongDoan);
            if (congDoan == null)
                return null;
            var lsMaySanXuat = BizMaySanXuat.GetList(congDoan.MaCongDoanSanXuat);
            if (lsMaySanXuat != null && lsMaySanXuat.Count > 0)
            {
                foreach (var maySanXuat in lsMaySanXuat)
                {
                    if (!allowAdmin)
                        if (!BizPhanCongBaoTri.CheckPermissionForMaySanXuat(manv, maySanXuat.MaMaySanXuat))
                            continue;

                    int trongHan = 0;
                    int quaHan = 0;
                    int truocHan = 0;
                    BizMaySanXuat.GetStatisCanhBao(maySanXuat.MaMaySanXuat, out quaHan, out trongHan, out truocHan, manv);
                    MaySanXuatCustom msx = new MaySanXuatCustom
                    {
                        MaCongDoanSanXuat = congDoan.MaCongDoanSanXuat,
                        MaMaySanXuat = maySanXuat.MaMaySanXuat,
                        TenMaySanXuat = maySanXuat.TenMaySanXuat,
                        CanhBao = string.Format("{0}/{1}/{2}", quaHan, trongHan, truocHan)
                    };
                    rs.Add(msx);
                }
            }
            return rs;
        }


        [WebMethod]
        public List<LichBaoTriCustom> GetListChiTietMay(string userName, string passWord, string maMaySanXuat)
        {
            var user = HeThong.GetNguoiSuDung(userName, passWord);
            if (user == null)
                return null;
            var manv = userName;
            bool allowAdmin = user.Role == (int)VAITRO.Admin;
            if (allowAdmin)
                manv = "";

            List<LichBaoTriCustom> ls = new List<LichBaoTriCustom>();
            int dsCanhBao = int.Parse(BizAppConfig.GetDSCanhBaoMobile());
            return BizLichBaoTri.GetList(dsCanhBao, maMaySanXuat, manv);
        }
    }
}
